package com.cup.bddm.impala.dao;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.ImpalaJdbc;
import com.cup.bddm.impala.po.BranchDailyTrans;
import com.cup.bddm.impala.po.BranchGeneDailyTrans;
import com.cup.bddm.util.DateUtil;

@Repository("nationDao")
public class NationDao {

	static Logger logger = LoggerFactory.getLogger(NationDao.class);

	@Autowired
	private ImpalaJdbc impalaJdbc;

	public List<BranchDailyTrans> getTop5Branch(String date, String type) {
		String nextDay = DateUtil.getNextDay(date);
		String sql = "";
		if (type.equals("nfc")) {
			sql = "select cup_branch_ins_id_cd,cup_branch_nm,nfc_trans_count as trans_count,nfc_trans_sum as trans_sum from " + Constant.TBL_BRANCH_GENERAL_DAY_TRANS + " where row_key>=? and row_key<? order by nfc_trans_count desc limit 5";
		} else if (type.equals("qrc")) {
			sql = "select cup_branch_ins_id_cd,cup_branch_nm,qrc_trans_count as trans_count,qrc_trans_sum as trans_sum from " + Constant.TBL_BRANCH_GENERAL_DAY_TRANS + " where row_key>=? and row_key<? order by qrc_trans_count desc limit 5";
		} else if (type.equals("offline")) {
			sql = "select cup_branch_ins_id_cd,cup_branch_nm,offline_trans_count as trans_count,offline_trans_sum as trans_sum from " + Constant.TBL_BRANCH_GENERAL_DAY_TRANS + " where row_key>=? and row_key<? order by offline_trans_count desc limit 5";
		} else {
			return null;
		}
		logger.info("exacute sql:" + sql + " . paremeter: pdate=" + date);
		List<BranchDailyTrans> branchDailyTransList = new ArrayList<BranchDailyTrans>();

		try {
			List<Map<String, Object>> list = impalaJdbc.queryForList(sql, date, nextDay);
			for (Map<String, Object> map : list) {
				BranchDailyTrans branchDailyTrans = new BranchDailyTrans();
				branchDailyTrans.setCup_branch_ins_id_cd(map.get("cup_branch_ins_id_cd").toString());
				branchDailyTrans.setCup_branch_nm(map.get("cup_branch_nm").toString());

				branchDailyTrans.setTrans_count((BigDecimal) map.get("trans_count"));
				branchDailyTrans.setTrans_sum((BigDecimal) map.get("trans_sum"));
				branchDailyTransList.add(branchDailyTrans);
			}

		} catch (SQLException e) {
			logger.error("error in query top 5 branch trans ", e);
			throw new RuntimeException("error in querytop 5 branch trans");
		}
		return branchDailyTransList;

	}

	public List<BranchGeneDailyTrans> getBranchGeneTrans(String date) {
		String nextDay = DateUtil.getNextDay(date);
		String sql = "SELECT * from " + Constant.TBL_BRANCH_GENERAL_DAY_TRANS + " where row_key>=? and row_key<?";

		logger.info("exacute sql:" + sql + " . paremeter: pdate=" + date);
		List<BranchGeneDailyTrans> branchGeneralTransList = new ArrayList<BranchGeneDailyTrans>();

		try {
			List<Map<String, Object>> list = impalaJdbc.queryForList(sql, date, nextDay);
			for (Map<String, Object> map : list) {
				BranchGeneDailyTrans generalTrans = new BranchGeneDailyTrans();
				generalTrans.setCup_branch_ins_id_cd(map.get("cup_branch_ins_id_cd").toString());
				generalTrans.setCup_branch_nm(map.get("cup_branch_nm").toString());
				generalTrans.setSq_count((BigDecimal) map.get("sq_count"));
				generalTrans.setJq_count((BigDecimal) map.get("jq_count"));
				generalTrans.setMchnt_count((BigDecimal) map.get("mchnt_count"));
				generalTrans.setAll_trans_count((BigDecimal) map.get("all_trans_count"));
				generalTrans.setAll_trans_sum((BigDecimal) map.get("all_trans_sum"));
				generalTrans.setNfc_trans_count((BigDecimal) map.get("nfc_trans_count"));
				generalTrans.setNfc_trans_sum((BigDecimal) map.get("nfc_trans_sum"));
				generalTrans.setQrc_trans_count((BigDecimal) map.get("qrc_trans_count"));
				generalTrans.setQrc_trans_sum((BigDecimal) map.get("qrc_trans_sum"));
				generalTrans.setOffline_trans_count((BigDecimal) map.get("offline_trans_count"));
				generalTrans.setOffline_trans_sum((BigDecimal) map.get("offline_trans_sum"));
				generalTrans.setPdate(map.get("pdate").toString());
				branchGeneralTransList.add(generalTrans);
			}

		} catch (SQLException e) {
			logger.error("error in query branch general trans info", e);
			throw new RuntimeException("error in query branch general trans info");
		}
		return branchGeneralTransList;

	}

}
